﻿using System;
using System.Collections.Generic;
using System.Drawing;
using Smartkernel;
using Smartkernel.Attributes;
using NUnit.Framework;

namespace Smartkernel.NUnitTest.
{
    [Category("Core")]
    [TestFixture]
    public class SmartReflectionTest
    {
        [Test]
        public void Test()
        {
            //【01】通过反射调用指定对象的指定方法（涉及方法：MethodInvoke）
            Type type = typeof(TempClass);
            Assert.AreEqual((int)SmartReflection.MethodInvoke("Fun1", new TempClass()) == 1, true);
            Assert.AreEqual((string)SmartReflection.MethodInvoke("Fun2", new TempClass(), new object[] { "世界你好" }) == "世界你好", true);
            Assert.AreEqual((int)SmartReflection.MethodInvoke("Fun1", type) == 1, true);
            Assert.AreEqual((string)SmartReflection.MethodInvoke("Fun2", type, new object[] { "世界你好" }) == "世界你好", true);

            //【02】获得成员信息（涉及方法：GetProperties、GetFields、GetPropertiesWithAttribute、GetFieldsWithAttribute）
            Assert.AreEqual(SmartReflection.GetProperties(type).Length > 0, true);
            Assert.AreEqual(SmartReflection.GetFields(type).Length > 0, true);
            Assert.AreEqual(SmartReflection.GetPropertiesWithAttribute<SmartXmlMappingAttribute>(type).Length > 0, true);
            Assert.AreEqual(SmartReflection.GetFieldsWithAttribute<SmartXmlMappingAttribute>(type).Length == 0, true);

            //【03】类型转换测试（涉及方法：ConvertTo）
            SmartReflection.ConvertTo<Color>("#FFFFFF").ToString();
            Assert.AreEqual(SmartReflection.ConvertTo<List<string>>("1|2|3|4|5").Count, 5);
            Assert.AreEqual(SmartReflection.ConvertTo<DateTime>("2008-01-02"), DateTime.Parse("2008-01-02"));
            Assert.AreEqual(SmartReflection.ConvertTo<TimeSpan>("10000").TotalSeconds, 10);
            Assert.AreEqual(SmartReflection.ConvertTo<decimal>("125.123"), 125.123M);
            Assert.AreEqual(SmartReflection.ConvertTo<DayOfWeek>("Saturday"), DayOfWeek.Saturday);

            //【04】对象的克隆（涉及方法：Clone）
            TempClass source = new TempClass();
            source.ID = 10;
            source.Name = "张三";
            TempClass item = SmartReflection.Clone<TempClass>(source);
            Assert.AreEqual(item.ID == source.ID, true);
            Assert.AreEqual(item.Name == item.Name, true);
        }

        private class TempClass
        {
            private string name;

            [SmartXmlMapping("Name")]
            public string Name
            {
                get { return this.name; }
                set { this.name = value; }
            }

            private int age;

            [SmartXmlMapping("Age")]
            public int Age
            {
                get { return this.age; }
                set { this.age = value; }
            }

            private string sex;

            [SmartXmlMapping("Sex")]
            public string Sex
            {
                get { return this.sex; }
                set { this.sex = value; }
            }

            private string phone;

            [SmartXmlMapping("Phone")]
            public string Phone
            {
                get { return this.phone; }
                set { this.phone = value; }
            }

            private string address;

            [SmartXmlMapping("Address")]
            public string Address
            {
                get { return this.address; }
                set { this.address = value; }
            }

            private int id;

            [SmartXmlMapping("ID")]
            public int ID
            {
                get { return this.id; }
                set { this.id = value; }
            }

            private int Fun1()
            {
                return 1;
            }
            private string Fun2(string message)
            {
                return message;
            }
        }
    }
}
